﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net;
using System.Collections.Specialized;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement
{
    public partial class _Default : BasePage
    {
        public _Default()
            : base("MAIN")
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            BinModule();
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                BinTreeFunction("List", "Danh mục");
            }


        }
        private void BinModule()
        {
            if (FSSContext.Current.User == null || FSSContext.Current.User.RoleId <= 0) return;

            ModuleEntity moduleE = new ModuleEntity();
            moduleE.RoleId = FSSContext.Current.User.RoleId;

            ModuleService moduleService = ModuleService.GetInstance();
            IList<ModuleEntity> lstModule = moduleService.LoadModuleHasChild(moduleE);
            if (lstModule == null) return;
            foreach (ModuleEntity module in lstModule)
            {
                Ext.Net.Button button = new Ext.Net.Button();
                button.Text = module.ModuleName;
                button.ID = "btn" + module.ModuleId;
                button.StateID = module.ModuleId;
                button.IconCls = module.ModuleId.ToLower();
                button.DirectClick += new ComponentDirectEvent.DirectEventHandler(button_DirectClick);
                button.Listeners.Click.Fn = "clearNode";
                button.Scale = ButtonScale.Large;
                button.IconAlign = IconAlign.Left;

                tbModule.Items.Add(button);
            }

        }
        protected void btnList_Click(object sender, DirectEventArgs e)
        {


        }

        protected void button_DirectClick(object sender, DirectEventArgs e)
        {

            Ext.Net.Button btn = (Ext.Net.Button)sender;
            BinTreeFunction(btn.StateID, btn.Text);
        }
        private void BinTreeFunction(string moduleId, string moduleName)
        {
            if (FSSContext.Current.User == null || FSSContext.Current.User.RoleId <= 0) return;
            //Ext.Net.TreeNode root = new Ext.Net.TreeNode(moduleName);
            Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();

            ScreenEntity screenE = new ScreenEntity();
            screenE.RoleId = FSSContext.Current.User.RoleId;
            screenE.Module = moduleId;

            ScreenService screenService = ScreenService.GetInstance();
            IList<ScreenEntity> screens = screenService.LoadScreenByModuleAndRole(screenE);

            if (screens == null)
                return;
            List<ScreenEntity> screensRoot = GetScreenByParentId(screens, moduleId);
            foreach (ScreenEntity screen in screensRoot)
            {
                Ext.Net.TreeNode screenNode = new Ext.Net.TreeNode(screen.ScreenName, Icon.Application);
                screenNode.Leaf = false;
                screenNode.NodeID = string.Format("{0}/Modules/{1}/{2}.{3}?id={4}", this.HostName, screen.Module, screen.FileName, screen.Extention, screen.ScreenId);
                screenNode.Text = screen.ScreenName;
                screenNode.Expanded = true;
                treeNavigator.AppendChild("root", screenNode);
                List<ScreenEntity> screensChild = GetScreenByParentId(screens, screen.ScreenId);
                foreach (ScreenEntity screenChild in screensChild)
                {
                    Ext.Net.TreeNode screenNodeChild = new Ext.Net.TreeNode(screenChild.ScreenName, Icon.Application);
                    screenNodeChild.Leaf = false;
                    screenNodeChild.NodeID = string.Format("{0}/Modules/{1}/{2}.{3}?id={4}", this.HostName, screenChild.Module, screenChild.FileName, screenChild.Extention, screenChild.ScreenId);
                    screenNodeChild.Text = screenChild.ScreenName;
                    treeNavigator.AppendChild(screenNode.NodeID, screenNodeChild);
                }
            }

        }
        private void BinChildFunction(IList<ScreenEntity> screens, string parent, Ext.Net.TreeNode parentNode)
        {
            foreach (ScreenEntity screen in screens)
            {
                if (screen.Parent != null && screen.Parent.Equals(parent))
                {
                    Ext.Net.TreeNode screenNode = new Ext.Net.TreeNode(screen.ScreenName, Icon.Application);
                    screenNode.Leaf = true;
                    screenNode.NodeID = string.Format("{0}/Modules/{1}/{2}.{3}", this.HostName, screen.Module, screen.FileName, screen.Extention);
                    screenNode.Text = screen.ScreenName;
                    parentNode.Nodes.Add(screenNode);
                    screens.Remove(screen);
                }
            }
        }
        private List<ScreenEntity> GetScreenByParentId(IList<ScreenEntity> screens, string ParentId)
        {
            List<ScreenEntity> newListScreen = new List<ScreenEntity>();
            foreach (ScreenEntity screen in screens)
            {
                if (screen.Parent != null && screen.Parent.Equals(ParentId))
                {
                    newListScreen.Add(screen);
                }

            }
            return newListScreen;
        }
        protected void btnWareHouse_Click(object sender, DirectEventArgs e)
        {

        }
        public string HostName
        {
            get
            {
                System.Uri url = Request.Url;
                string hostname = url.Scheme + "://" + url.Authority.ToLower();
                return hostname;
            }
        }



    }

}
